package com.zja.dao.Impl;

import com.zja.dao.BaseDAO;
import com.zja.dao.OrganizationMemberDao;
import com.zja.model.OrganizationMember;

import java.util.List;

public class OrganizationMemberDaoImpl extends BaseDAO implements OrganizationMemberDao {
    @Override
    public List<OrganizationMember> selectAll() {
        String sql = "SELECT * FROM organization_member";
        try {
            return executeQuery(OrganizationMember.class, sql, null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public OrganizationMember selectById(Long id) {
        String sql = "SELECT * FROM organization_member WHERE id = ?";
        try {
            return executeQueryBean(OrganizationMember.class, sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean insert(OrganizationMember organizationMember) {
        String sql = "INSERT INTO organization_member(organization_id, user_id, role, created_at) VALUES(?,?,?,?)";
        try {
            return executeUpdate(sql, organizationMember.getOrganizationId(), organizationMember.getUserId(),
                    organizationMember.getRole(), organizationMember.getCreatedAt());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean update(OrganizationMember organizationMember) {
        String sql = "UPDATE organization_member SET organization_id = ?, user_id = ?, role = ?, WHERE id = ?";
        try {
            return executeUpdate(sql, organizationMember.getOrganizationId(), organizationMember.getUserId(),
                    organizationMember.getRole(),  organizationMember.getId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean deleteById(Long id) {
        String sql = "DELETE FROM organization_member WHERE id = ?";
        try {
            return executeUpdate(sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
